package Client;

import Car.Car.Car;
import Information.RentalInformation;
import InputConstraint.AnswerOfInt;
import InputConstraint.AnswerOfString;
import ManipullateDatabases.OperatorCType;
import ManipullateDatabases.OperatorCar;
import OperatOfXML.AnalysisXML;
import OperatOfXML.CreateXML;
import org.junit.Test;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

public class Client {
    private List<Car> cars;
    private Map<String,String> cType1 = null;
    private Map<String,String>cType2 = null;

    private double piece;

    private double getPiece() {
        return piece;
    }

//    public void initMap(){
//        cType1 = new HashMap<>();
//        cType1.put("货车","Hc");
//        cType1.put("客车","Kc");
//        cType1.put("皮卡","Pkc");
//        System.out.println(cType1.get("货车"));
//    }
    public void setPiece(double piece) {
        this.piece += piece;
    }

    public Client() {
        this.piece = 0;
        //AnalysisXML analysis = new AnalysisXML();
        //analysis.insertInSQL();
    }

    public void outPrice() {

        System.out.println("您需要支付" + getPiece() + "元!");
    }
    public  void Interface() {
        cType2 =new HashMap<>();
        List<RentalInformation> informations = new ArrayList<RentalInformation>();
        DateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");
        List<Car> RentalCar = null;
        Client client = new Client();
        System.out.println("欢迎来到租车系统!");
        System.out.println("是否进入租车系统?(yes/no)");
        String login = AnswerOfString.getString2();
        if (login.equals("yes")) {
            while (true) {
                System.out.println("车辆的类型:");
                List<String> types = OperatorCType.getCatType();
                Integer i=0;
                for(String s:types){
                    System.out.println(""+(++i)+":"+s);
                    cType2.put(String.valueOf(i),s);
                }
                System.out.println("4.退出系统。");
                int id = -1;
                while (true) {
                    System.out.println("请选择您要租车的类型");
                    id = AnswerOfInt.RangeOfNumber(1, 4);
                    if (id == 4) {
                        break;
                    }
                    String name = cType2.get(String.valueOf(id)).trim();
                    cars = OperatorCar.selectCars(name);
                    System.out.println(cars.size());
                    for(int j=0;j<cars.size();j++){
                        System.out.println("id "+(j+1)+":"+cars.get(j));
                    }
                    System.out.println("请选择您要租的车");
                    int cId = new AnswerOfInt().RangeOfNumber(1,cars.size());
                    System.out.println(cars.size());
                    
                    Car tempCar = cars.get(cId-1);
                    System.out.println("请选择您要租赁的天数");
                    int day = new AnswerOfInt().RangeOfNumber(1, 3);
                    client.setPiece(day * (tempCar.getPrice()));
                    Date date = new Date();
                    RentalInformation temp = new RentalInformation(tempCar.getName(), day, day * (tempCar.getPrice()), dateFormat.format(date));
                    informations.add(temp);
                    System.out.println("是否返回上一层?yes/no");
                    String ret = AnswerOfString.getString2();
                    if (ret.equals("yes")) {
                        break;
                    }
                }
                client.outPrice();
                System.out.println("是否退出程序?yes/no");
                String ret = AnswerOfString.getString2();
                if (ret.equals("yes")) {
                    break;
                }
            }
            new CreateXML().insertInformationOfXML(informations);
        }
    }
}
